<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller
{

    /**
     * 后台用户登录
     */
    public function login()
    {
        if (IS_POST) {
            $username = I('post.username');
            $password = I('post.password');
            $captcha = I('post.captcha');
            if (! checkVerify($captcha)) {
                $this->error('验证码输入错误！');
            }
            
            $result = D('Admin')->login($username, $password);
            if ($result > 0) {
                redirect(U('admin/index/index'));
            } else {
                switch ($result) {
                    case - 1:
                        $error = '用户不存在或被禁用！';
                        break;
                    case - 2:
                        $error = '密码错误！';
                        break;
                    default:
                        $error = '未知错误';
                        break;
                }
                $this->error($error);
            }
        } else {
            if (isAdminLogin()) {
                redirect(U('admin/index/index'));
            } else {
                $this->display();
            }
        }
    }

    /**
     * 退出登陆
     */
    public function logout()
    {
        if (isAdminLogin()) {
            D('Admin')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function captcha()
    {
        $config = array(
            'fontSize' => 14, // 验证码字体大小(px)
            'useCurve' => true, // 是否画混淆曲线
            'useNoise' => true, // 是否添加杂点
            'imageH' => 26, // 验证码图片高度
            'imageW' => 90, // 验证码图片宽度
            'length' => 4, // 验证码位数
            'fontttf' => '4.ttf'
        );
        $verify = new \Think\Verify($config);
        $verify->entry(1);
    }
    
    public function t()
    {
        $a = D('Member')->find();
        dump($a);
        S('test', 'hello!');
        dump(S('test'));
    }
    
    public function p()
    {
        dump($_SESSION);
        phpinfo();
    }
}